package ch.bfh.mobicompsoda2.sms_sender.sender_service;

import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.IBinder;
import android.telephony.SmsManager;
import android.util.Log;
import ch.bfh.mobicompsoda2.sms_sender.sender_service.utils.SODA2_Json_Converter;

public class SODA_Send_SMS_Service extends Service {

    private static final String TAG = SODA_Send_SMS_Service.class.getName();

    private static final String MULTI_MODULE_TAG = "MULTISP";

    private static final String SMS_DEFAULT_SOURCEADRESS = "0123456789";
    private static final String SMS_SOURCEADRESS = System.getProperty(
            "android.telephony.TelephonyProperties.PROPERTY_IMSI", SMS_DEFAULT_SOURCEADRESS);

    public static final String SEND_SMS_ACTION = "SEND_SODA_SMS";
    public static final String SEND_SMS_EXTRA_NUMBER = "SEND_SODA_SMS_EXTRA_NUMBER";
    public static final String SEND_SMS_EXTRA_TAG = "SEND_SODA_SMS_EXTRA_TAG";
    public static final String SEND_SMS_EXTRA_MESSAGETEXT = "SEND_SODA_SMS_EXTRA_MESSAGETEXT";
    public static final String SEND_SMS_EXTRA_SP_NAMES = "SEND_SODA_SMS_EXTRA_SP_NAMES";

    public static final String SODA_SEND_PENDINGINTENT_ACTION = "SODA_SEND_INTENT_ACTION";
    public static final String SODA_DELIVERY_PENDINGINTENT_ACTION = "SODA_DELIVERY_INTENT_ACTION";

    public static final String SHARED_PREFEENCES_JSON_TAG = "JSON";
    public static final String DEFAULT_SHARED_PREFEENCES_JSON_TEXT = "NJF";

    private static Intent SENDSMSINTENT = new Intent(SEND_SMS_ACTION);

    String receiverNumber = "";
    String moduleTag = "";
    String spList = "";
    String fullMessageText = "";

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {

        receiverNumber = intent.getStringExtra(SEND_SMS_EXTRA_NUMBER);
        moduleTag = intent.getStringExtra(SEND_SMS_EXTRA_TAG);
        spList = intent.getStringExtra(SEND_SMS_EXTRA_SP_NAMES);

        fullMessageText = collectSPData(spList);

        sendSMS(receiverNumber, fullMessageText, moduleTag, spList);
        Log.d(TAG, "SMS gesendet an: " + receiverNumber);

        return Service.START_STICKY;
    }

    private String collectSPData(String commaSeparatedSP) {
        String fullText = "";
        Log.d(TAG, "CommaSeparatedSP: "+commaSeparatedSP);
        if (commaSeparatedSP != null) {
            String[] spList = commaSeparatedSP.split(",");
            for (String sp : spList) {
                fullText += getJsonFromSharedPref(sp);
            }
        }
        return fullText;
    }

    private String getJsonFromSharedPref(String sharedpreferencesName) {
        Log.d(TAG, "getJsonFromSharedPref "+sharedpreferencesName);
        SharedPreferences preferences = getSharedPreferences(sharedpreferencesName, MODE_PRIVATE);
        String json = preferences.getString(SHARED_PREFEENCES_JSON_TAG, DEFAULT_SHARED_PREFEENCES_JSON_TEXT);
        Log.d(TAG, "getJsonFromSharedPref JSON: "+ json);
        // in Ermangelung eines besseren Namens... ;-P
        String schaerNotation = new SODA2_Json_Converter().convertJson2SchaerNotation(json);
        Log.d(TAG, "schaerNotation: "+ schaerNotation);
        return schaerNotation;
    }

    private void sendSMS(String number, String text, String moduleTag, String shraredPreferencesname) {
        SmsManager manager = SmsManager.getDefault();
        manager.sendTextMessage(number, null, text,
                createNotificationIntent(SODA_SEND_PENDINGINTENT_ACTION, number, moduleTag, shraredPreferencesname),
                createNotificationIntent(SODA_DELIVERY_PENDINGINTENT_ACTION, number, moduleTag, shraredPreferencesname));
    }

    public PendingIntent createNotificationIntent(String action, String number, String moduleTag,
            String shraredPreferencesname) {
        Intent intent = new Intent(action);
        intent.putExtra(SEND_SMS_EXTRA_SP_NAMES, shraredPreferencesname);
        intent.putExtra(SEND_SMS_EXTRA_NUMBER, number);
        intent.putExtra(SEND_SMS_EXTRA_TAG, moduleTag);
        Log.d(TAG, "returning NotificationIntent "+ action);
        return PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
    }

    public static Intent getSendSMSIntentTest(String number, String moduleTag, String json) {
        SENDSMSINTENT.putExtra(SEND_SMS_EXTRA_MESSAGETEXT, json);
        SENDSMSINTENT.putExtra(SEND_SMS_EXTRA_NUMBER, number);
        SENDSMSINTENT.putExtra(SEND_SMS_EXTRA_TAG, moduleTag);
        Log.d(TAG, "returning SEND SMS INTENT:" + SENDSMSINTENT.toString());
        return SENDSMSINTENT;

    }

    public static Intent getSendSMSIntent(String number, String[] shraredPreferencesnames) {
        String nameList = "";
        for (String spn : shraredPreferencesnames) {
            nameList += spn + ",";
        }
        return getSendSMSIntent(number, MULTI_MODULE_TAG, nameList);
    }

    public static Intent getSendSMSIntent(String number, String moduleTag, String shraredPreferencesname) {
        SENDSMSINTENT.putExtra(SEND_SMS_EXTRA_SP_NAMES, shraredPreferencesname);
        SENDSMSINTENT.putExtra(SEND_SMS_EXTRA_NUMBER, number);
        SENDSMSINTENT.putExtra(SEND_SMS_EXTRA_TAG, moduleTag);
        Log.d(TAG, "returning SEND SMS INTENT:" + SENDSMSINTENT.toString());
        return SENDSMSINTENT;

    }

    public static Intent getStopSendIntent() {
        return SENDSMSINTENT;
    }

    @Override
    public IBinder onBind(Intent intent) {
        // TODO Auto-generated method stub
        return null;
    }
}
